use crate::helpers::count_ttr;
use db::Db;

use anyhow::Result;
// use db::models::MatrixOrTelegramId;
use matrix_sdk::ruma::events::room::message::RoomMessageEventContent;
use matrix_sdk::{room::Room, ruma::events::room::message::OriginalSyncRoomMessageEvent, Client};

pub async fn count_unread_time(
    ev: OriginalSyncRoomMessageEvent,
    _text: String,
    room: Room,
    _client: Client,
    db: Db,
) -> Result<()> {
    log::info!("counting unread time");
    let matrix_id = ev.sender.as_str();
    // let matrix_id = MatrixOrTelegramId::MatrixId(matrix_id);
    // let user_id = db.get_user_id(&matrix_id).await.unwrap();
    let word_count = db.count_unread_time_db_matrix(matrix_id).await?;
    let content = {
        if word_count.gt(&0) {
            let time = count_ttr(Some(word_count.into())).unwrap();
            RoomMessageEventContent::text_markdown(format!("Unread time: {time}"))
        } else {
            RoomMessageEventContent::text_markdown("No unread posts!")
        }
    };
    room.send(content).await?;

    Ok(())
}
